﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;

namespace Walson.Net
{
//     /// <summary>
//     /// 适配器类型
//     /// </summary>
//     enum AdapterTypes
//     {
//         /// <summary>
//         /// 无线适配器
//         /// </summary>
//         Wireless,
//         /// <summary>
//         /// 以太适配器
//         /// </summary>
//         Ethernet,
//         /// <summary>
//         /// 虚拟适配器
//         /// </summary>
//         Virtual,
//         /// <summary>
//         /// 回还适配器
//         /// </summary>
//         LoopBack
//     }

    /// <summary>
    /// 适配器
    /// </summary>
    public class Adapter
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get;
            private set;
        }

        /// <summary>
        /// 适配器的 MAC 地址
        /// </summary>
        public byte[] Mac
        {
            get;
            private set;
        }
        
        /// <summary>
        /// 适配器的 IP 地址
        /// </summary>
        public IPAddress IP
        {
            get;
            private set;
        }
        
        /// <summary>
        /// 适配器类型
        /// </summary>
        public System.Net.NetworkInformation.NetworkInterfaceType AdapterType
        {
            get;
            private set;
        }

        /// <summary>
        /// 适配器接口速率
        /// </summary>
        public long Speed
        {
            get;
            private set;
        }

        /// <summary>
        /// 适配器当前操作类型
        /// </summary>
        public System.Net.NetworkInformation.OperationalStatus OperationalStatus
        {
            get;
            private set;
        }

        /// <summary>
        /// 获取所有适配器
        /// </summary>
        /// <returns></returns>
        public static Adapter[] GetAllAdapters()
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();//获取本机所有网卡对象
            List<Adapter> adapterList = new List<Adapter>();
            foreach (NetworkInterface ni in adapters)
            {
                Adapter adapter = new Adapter();
                adapter.AdapterType = ni.NetworkInterfaceType;
                IPInterfaceProperties ipp = ni.GetIPProperties();
                if (ipp.UnicastAddresses.Count > 0)
                {
                    foreach(IPAddressInformation ipi in ipp.UnicastAddresses)
                    {
                        if(ipi.Address.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            adapter.IP = ipi.Address;
                            break;
                        }
                    }
                    
                }
                adapter.Mac = ni.GetPhysicalAddress().GetAddressBytes();
                adapter.Name = ni.Name;
                adapter.OperationalStatus = ni.OperationalStatus;
                adapter.Speed = ni.Speed;
                adapterList.Add(adapter);
            }
            return adapterList.ToArray();
        }

        /// <summary>
        /// 适配器
        /// </summary>
        public Adapter()
        {

        }
    }
}
